package com.seguim.geroglifics;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileFilter;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class GeroglificHelper {
	private static final String ICON_FILE_NAME_SUFIX = ".gif";
	private static final String UNKNOWN_ICON_NAME = "desconegut";
	private static final Color BACKGROUND_COLOR = Color.WHITE;
	
	private String blankIconName;
	private int unknownIconPosition = -1;
	
	private ImageIcon[] icons = null;
	public GeroglificHelper(File directori, String blankIconName) {
		this.blankIconName = blankIconName;
		File[] files = directori.listFiles(new FileFilter() {
			public boolean accept(File pathname) {
				return pathname.canRead() && pathname.getName().endsWith(ICON_FILE_NAME_SUFIX);
			}});
		icons = new ImageIcon[files.length];
		boolean foundBlank = false;
		for (int i = 0; i < files.length; i++) {
			icons[i] = new ImageIcon(files[i].getAbsolutePath(), files[i].getName().substring(0, files[i].getName().length()-ICON_FILE_NAME_SUFIX.length()));
			if (icons[i].getDescription().equals(UNKNOWN_ICON_NAME)) {
				unknownIconPosition = i;
			}
			if (icons[i].getDescription().equals(blankIconName)) {
				foundBlank = true;
			}
		}
		if (unknownIconPosition == -1) {
			throw new RuntimeException(directori.getAbsolutePath() + " must contain a file named " + UNKNOWN_ICON_NAME + ICON_FILE_NAME_SUFIX);
		}
		if (!foundBlank) {
			throw new RuntimeException(directori.getAbsolutePath() + " must contain a file named " + blankIconName + ICON_FILE_NAME_SUFIX);
		}
	}
	public Symbol getSimbol(String nom) {
		if (nom.equals(" ")) {
			nom = blankIconName;
		}
		return new Symbol(nom, findIcon(nom));
	}
	

	public Geroglific nomToGeroglific(String nom) {
		Geroglific geroglific = new Geroglific();
		Row fila = null;
		Col columna = null;
		
		fila = new Row();
		geroglific.addRow(fila);
		for (int i = 0; i < nom.length(); i++) {
			char caracter = nom.charAt(i);
			if (caracter != 'e' || columna == null) {

				columna = new Col();
				fila.addCol(columna);	
			}
			
			
			columna.addSymbol(getSimbol(String.valueOf(caracter)));
		}
		
		return geroglific;
		
	}
	public Geroglific nomToGeroglificOld(String nom) {
		Geroglific geroglific = new Geroglific();
		
		Row fila = new Row();
		geroglific.addRow(fila);
		
		Col columna = new Col();
		fila.addCol(columna);
		
		columna.addSymbol(getSimbol("g"));
		columna.addSymbol(getSimbol("e"));

		columna = new Col();
		fila.addCol(columna);
		
		columna.addSymbol(getSimbol("m"));
		
		columna = new Col();
		fila.addCol(columna);
		
		columna.addSymbol(getSimbol("m"));
		

		columna = new Col();
		fila.addCol(columna);
		
		columna.addSymbol(getSimbol("a"));
		
		return geroglific;
		
	}

	public BufferedImage createImage(Geroglific geroglific) {
		int imgWidth = geroglific.getWidth();
		int imgHeight = geroglific.getHeight();
		
		
		BufferedImage img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = (Graphics2D)img.getGraphics();
		g.setColor(BACKGROUND_COLOR);
		g.fillRect(0, 0, imgWidth, imgHeight);
		

		int rowY = 0;
		List rows = geroglific.getRows();
		for (Iterator rowIter = rows.iterator(); rowIter.hasNext();) {
			Row row = (Row) rowIter.next();
			int rowX = 0;			
			List cols = row.getCols();
			for (Iterator colIter = cols.iterator(); colIter.hasNext();) {
				Col col = (Col) colIter.next();
				
				int colY = 0;
				
				List symbols = col.getSymbols();
				for (Iterator symbolIter = symbols.iterator(); symbolIter.hasNext();) {
					Symbol symbol = (Symbol) symbolIter.next();
					ImageIcon icon = symbol.getIcon();
					
					int iconHeight = icon.getIconHeight();
					
					g.drawImage(icon.getImage(), rowX, colY + ((row.getHeigth() - col.getHeight()) / 2), null);
					
					colY += iconHeight;
				}

				rowX += col.getWidth();
			}
			rowY += row.getHeigth();
		}
		return img;
	}
	public ImageIcon findIcon(String description) {
		for (int i = 0; i < icons.length; i++) {
			if (icons[i].getDescription().equalsIgnoreCase(description)) {
				return icons[i];
			}
		}
		return icons[unknownIconPosition];
	}
	public BufferedImage createImage(String nom) {
		return createImage(nomToGeroglific(nom));
	}
	public void printImage(BufferedImage image, String format, OutputStream out) throws Exception {
		ImageIO.write(image, format, out);
	}
	public void printImage(String nom, String format, OutputStream out) throws Exception {
		printImage(createImage(nom), format, out);
	}
}
